<?php
class Application_Model_InfoMapper
{
	protected $_dbTable;
	
	public function setDbTable($dbTable)
	{
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}

	public function getDbTable()
	{
		if (null === $this->_dbTable) {
			$this->setDbTable('Application_Model_DbTable_Info');
		}
		return $this->_dbTable;
	}

	public function save(Application_Model_Info $Info)
	{
		$data = array(
			'title'   => $blog->getTitle(),
			'content' => $blog->getContent(),
			'create_dt' => date('Y-m-d H:i:s'),
		);
		
		if (null === ($id = $blog->getId())) {
			unset($data['id']);
			$this->getDbTable()->insert($data);
		} else {
			$this->getDbTable()->update($data, array('id = ?' => $id));
		}
	}
	
	public function selectNewest(Application_Model_Info $info)
	{
		$row = $this->getDbTable()->fetchRow('username = "ghold"','create_dt DESC');
		if (0 == count($row)) {
			return;
		}
		$info->setId($row->id)
			 ->setStatus($row->status)
			 ->setContact($row->contact);
	}

    public function fetchAll()
    {
        $resultInfo = $this->getDbTable()->fetchAll();
        $entries   = array();
        foreach ($resultInfo as $row) {
            $entry = new Application_Model_Info();
            $entry->setId($row->id)
                  ->setStatus($row->status)
                  ->setContact($row->contact);
            $entries[] = $entry;
        }
        return $entries;
    }
}
